var xmlHttp=false;
function GetXmlHttpObject()
{
	try
	 {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
			// Internet Explorer
		 try
			  {
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
		 catch (e)
			  {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
	 }
	 if(!xmlHttp)
	 {
		alert("无法创建对象!");
	 }
}//创建对象
function firstenter()
{
	var myXmlHttp;
	var url='http://herald.seu.edu.cn/AccountCenter/SignOn';
	try
	 {
		// Firefox, Opera 8.0+, Safari
		myXmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
			// Internet Explorer
		 try
			  {
				myXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
		 catch (e)
			  {
				myXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
	 }
	 if(!myXmlHttp)
	 {
		alert("无法创建对象!");
	 }
	myXmlHttp.open("GET",url,true);
	myXmlHttp.onreadystatechange=function deep(){};
	myXmlHttp.send(null);
}
function enter()
{
	var username=document.loginForm.email.value;
	var password=document.loginForm.password.value;
	var userType=loginForm.select.value;
	for(var i=0;i<loginForm.select.length;i++)
	{
	   if(loginForm.select[i].checked==true)
		{
		  userType = loginForm.select[i].value;
		  break;
		}
    }
	if(username && password)
	{
		if(userType == "user")
		{
			firstenter();
			cnWait(1);
			var url='http://herald.seu.edu.cn/AccountCenter/SignOn?username=' + username + '&password=' + password;
			GetXmlHttpObject();//实例化对象
			xmlHttp.open("GET",url,true);
			xmlHttp.onreadystatechange=function deepOutPut()
			{
				if (xmlHttp.readyState==4 && xmlHttp.status==200)
				{
					var getStr=xmlHttp.responseText;
					if(getStr.length < 500 )
					{
						var url="http://herald.seu.edu.cn/league_p/insert.php";
						url=url+"?password="+password;
						GetXmlHttpObject();//实例化对象
						xmlHttp.open("GET",url,true);
						xmlHttp.onreadystatechange =function fin()
						{
							
							if (xmlHttp.readyState==4 && xmlHttp.status==200)
							{
								var getStr=xmlHttp.responseText;
							    var re=new Array();
							    re = getStr.split(",");
								if(getStr == "fail")
								{
									alert("系统错误");
								}
							location.reload();
							}
					}
						
						xmlHttp.send(null);
					}
					else
					{
						alert("用户名或密码错误");
					}
				}
			}
			xmlHttp.send(null);
		}
		else if(userType == "league")
		{
			var url="http://herald.seu.edu.cn/league_p/enter.php";
			url=url+"?username="+username+"&password="+password;
			GetXmlHttpObject();//实例化对象
			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
			xmlHttp.onreadystatechange=function output()
			{
				if (xmlHttp.readyState==4 && xmlHttp.status==200)
				{ 
					var getStr=xmlHttp.responseText;
					var result=new Array();
					result = getStr.split(",");
					switch(result[0])
					{
						case "lg_success":
							var u = window.location.pathname;
							if(u != "/league_p/index.php")
							{
								location.reload();
							}else{
								document.getElementById("cardNum").innerHTML="<span class='userInfor'>"+re[1]+"</span>";
								document.getElementById("loginButton").style.display='none';
								document.getElementById("head_logout").innerHTML="<span class='userInfor'>退出</span>";
								document.getElementById("loginBox").innerHTML="";
							}
							break;
						case "blank":
							alert("用户名或密码为空");
							break;
						case "fail":
							alert("用户名或密码错误");
							break;
					}
				}
			}
		}
	}
	else
	{
		alert("用户名或密码为空");
	}
}
function cnWait (second)
{
	var startTime,endTimes,s; 
	var d=new Date(); 
	startTime=d.getTime(); 
	while(true)
	{
		d=new Date();
		endTime=d.getTime();
		s = (endTime-startTime)/1000;
		if (s >= second)
		  break;
	}
}